	.386
	.model flat,stdcall
	option casemap:none

	include 	windows.inc
	include 	gdi32.inc
	includelib	gdi32.lib
	include		user32.inc
	includelib	user32.lib
	include 	kernel32.inc
	includelib	kernel32.lib
	include 	shell32.inc 
	includelib	shell32.lib

	WM_SHELLNOTIFY equ WM_USER+5	;
	ICO_MAIN equ 1000h		;图标
	IDI_TRAY equ 1001		;托盘消息uid
	IDM_EXIT equ 1002
    IDM_HIDE equ 1003


	.data
	hInstance 		dd ?	;当前程序句柄
	hWinMain		dd ?	;窗口句柄
	hPopupMenu       dd 0	;快捷菜单句柄
	pt               POINT <>
	HideString       db "H&ide Desktop",0
    ExitString       db "E&xit Program",0
    note             NOTIFYICONDATA <>

	.const
	szClassName 	db	'mywindow',0
	szCaptionMain	db	'This is a Window Title',0
	szText			db	'This my first window ,do you have any problem ?',0
	szMessage		db	'双击了'
	.code

;//

	addNote	proc

				mov note.cbSize,sizeof NOTIFYICONDATA 
                push hWinMain 
                pop note.hwnd 
                mov note.uID,IDI_TRAY 
                mov note.uFlags,NIF_ICON+NIF_MESSAGE+NIF_TIP 
                mov note.uCallbackMessage,WM_SHELLNOTIFY  
                invoke LoadIcon,hInstance,ICO_MAIN 
                mov note.hIcon,eax 
                invoke lstrcpy,addr note.szTip,addr szCaptionMain 
                 
                invoke Shell_NotifyIcon,NIM_ADD,addr note 
	addNote endp

	winNext proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
		local	@stPs:PAINTSTRUCT	;绘图结构
		local	@stRect:RECT		;坐标结构
		local	@hDc				;显示设备描述表句柄。
		
		mov		eax,uMsg
		
		.if eax == WM_CREATE
			;设置标题图标
			invoke	LoadIcon,hInstance,ICO_MAIN
			invoke	SendMessage,hWnd,WM_SETICON,ICON_BIG,eax

			invoke CreatePopupMenu 
            mov hPopupMenu,eax 
            invoke AppendMenu,hPopupMenu,MF_STRING,IDM_HIDE,addr HideString 
            invoke AppendMenu,hPopupMenu,MF_STRING,IDM_EXIT,addr ExitString 
            invoke SendMessage,hWinMain,WM_SIZE,SIZE_MINIMIZED,0
        	invoke	addNote
         .elseif uMsg == WM_SHELLNOTIFY 
            .if wParam == IDI_TRAY 
                ;--------------------------------------------------------
                ; Display the popup menu at the specified location when
                ; the user right clicks on the applications tray icon.
                ;--------------------------------------------------------
                .if lParam == WM_RBUTTONDOWN or WM_RBUTTONUP 
                    invoke GetCursorPos,addr pt 
                    invoke SetForegroundWindow,hWinMain 
                    invoke TrackPopupMenu,hPopupMenu,TPM_RIGHTALIGN,pt.x,pt.y,0,hWinMain,0 
                    invoke PostMessage,hWinMain,WM_NULL,0,0 
                .elseif lParam==WM_LBUTTONDBLCLK
                	invoke ShowWindow,hWinMain,SW_SHOWNORMAL
                	
                .endif 
            .endif
   		
		.elseif		eax==WM_PAINT
			

			;BeginPaint函数为指定窗口进行绘图工作的准备,
			;并用将和绘图有关的信息填充到一个PAINTSTRUCT结构中。
			invoke	BeginPaint,hWnd,addr @stPs
			mov		@hDc,eax

			;获取窗口客户区的坐标
			invoke	GetClientRect,hWnd,addr @stRect

			;该函数在指定的矩形里写入格式化的正文
			invoke	DrawText,@hDc,addr szText,-1,\
					addr @stRect,\
					DT_SINGLELINE or DT_CENTER or DT_VCENTER
			
			;结束绘图
			invoke	EndPaint,hWnd,addr @stPs


		.elseif	eax==WM_CLOSE
			invoke ShowWindow,hWinMain,SW_HIDE
			invoke addNote
   		.elseif eax==WM_DESTROY
   			invoke  DestroyWindow,hWinMain  ;销毁指定的窗口
   		    invoke  PostQuitMessage,NULL    ;该函数向系统表明有个线程有终止请求

   		.elseif uMsg == WM_COMMAND 
            ;--------------------------------------------------------
            ; Handles the applications tray icon popup menu options.
            ;--------------------------------------------------------
            .if lParam == 0 
                .if wParam == IDM_EXIT
                    invoke Shell_NotifyIcon,NIM_DELETE,addr note 
                    invoke SendMessage,hWinMain,WM_DESTROY,0,0
                .elseif wParam == IDM_HIDE 
                    invoke	ShowWindow,hWinMain,SW_SHOWNORMAL
                .endif 
            .endif
   		.else
   			invoke	DefWindowProc,hWnd,uMsg,wParam,lParam		;默认的窗口处理函数
   			ret
		.endif
		xor eax,eax
		ret
	winNext endp

;/创建window主程序
	winMain proc
		local @myWindow:WNDCLASSEX
		local @myMsg:MSG

		invoke	GetModuleHandle,NULL	;获取本模块的句柄
		mov		hInstance,eax			;函数的返回值，在eax寄存器中，
										;从eax中取值，放入hInstance中
	;//注册窗口开始
		invoke	RtlZeroMemory,addr @myWindow,sizeof @myWindow	;清0 @mywindow占用的内存

		invoke	LoadCursor,0,IDC_ARROW	;从指定的模块或应用程序实例中载入一个鼠标指针
		mov		@myWindow.hCursor,eax	;将LoadCursor的返回值，放入hCursor中

		push	hInstance				;压hInstance到栈
		pop		@myWindow.hInstance		;将hInstance挤到@mywindow.hInstance中

		;为myWindow设置值
		mov		@myWindow.cbSize,sizeof WNDCLASSEX
		mov		@myWindow.style,CS_HREDRAW or CS_VREDRAW
		mov		@myWindow.lpfnWndProc,offset winNext
		mov		@myWindow.hbrBackground,COLOR_WINDOW+1
		mov		@myWindow.lpszClassName,offset szClassName

		;注册窗口
		invoke	RegisterClassEx,addr @myWindow

	;//注册窗口结束

	;//创建并显示窗口
		;//创建Window
		invoke	CreateWindowEx,WS_EX_TOOLWINDOW,offset szClassName,NULL,\
				WS_OVERLAPPEDWINDOW,\
				100,100,600,400,\
				NULL,NULL,hInstance,NULL
		mov		hWinMain,eax			;将创建的Window

		invoke	ShowWindow,hWinMain,SW_SHOWNORMAL	;显示窗口
		invoke	UpdateWindow,hWinMain				;强制立即更新窗口，窗口中以前屏蔽的所有区域都会重画

	;//创建并显示窗口结束

	
	;//消息循环
		.while	TRUE
			invoke	GetMessage,addr @myMsg,NULL,0,0	;检查消息，如果有消息回调窗口处理过程
			.break	.if 	eax==0
			invoke	TranslateMessage,addr @myMsg	;预警消息
			invoke	DispatchMessage,addr @myMsg		;发一个消息给窗口程序
		.endw

	;//消息循环结束

		ret
	winMain endp

	start:
		call	winMain 			;执行过程
		invoke	ExitProcess,NULL	;退出程序
		end start



